package com.bxd.baodan.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.item.MultiItem;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;

import java.util.ArrayList;
import java.util.List;

public class SelectMultiSortActivity extends BaseActivity {
    private ExpandableListView expandableListView;
    List<MultiItem.DataBean.ListBean> data = new ArrayList<>();

    private BaseExpandableListAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_sort);
        setActionBarTitle("产品对比");


        NetUtil netUtil = new NetUtil("product/categorylist", this);
        netUtil.postRequest("", new RequestStringListener() {
            @Override
            public void onComplete(String result) {
                if (JSONUtil.isSuccess(result)) {
                    MultiItem item = new Gson().fromJson(result, MultiItem.class);
                    data.addAll(item.data.list);
                    adapter.notifyDataSetChanged();
                }
            }
        });


        adapter = new BaseExpandableListAdapter() {
            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return false;
            }

            @Override
            public int getGroupCount() {
                return data.size();
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return data.get(groupPosition).subType.size();
            }

            @Override
            public Object getGroup(int groupPosition) {
                return data.get(groupPosition);
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return data.get(groupPosition).subType.get(childPosition);
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return false;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(SelectMultiSortActivity.this, R.layout.left_right_item, null);
                }
                ((ImageView) convertView.findViewById(R.id.left_iv)).setImageResource(R.drawable.p_compare);
                ((ImageView) convertView.findViewById(R.id.right_iv)).setImageResource(isExpanded?R.drawable.p_shang:R.drawable.p_xia);
                ((TextView) convertView.findViewById(R.id.left_tv)).setText(data.get(groupPosition).typeName);


                return convertView;
            }

            @Override
            public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(SelectMultiSortActivity.this, R.layout.left_right_item, null);
                }
                ((ImageView) convertView.findViewById(R.id.left_iv)).setImageResource(R.color.transparent);
                ((ImageView) convertView.findViewById(R.id.right_iv)).setImageResource(R.color.transparent);
                convertView.findViewById(R.id.line).setBackground(getResources().getDrawable(R.color.white));
                convertView.findViewById(R.id.l_r_layout).setBackground(getResources().getDrawable(R.color.gray_f4));
                ((TextView) convertView.findViewById(R.id.middle_tv)).setTextColor(getResources().getColor(R.color.question_list_text_2));
                ((TextView) convertView.findViewById(R.id.middle_tv)).setText(data.get(groupPosition).subType.get(childPosition).typeName);

                convertView.findViewById(R.id.l_r_layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

                return convertView;
            }


        };


        expandableListView = (ExpandableListView) findViewById(R.id.expand_lv);
        expandableListView.setGroupIndicator(null);
        expandableListView.setAdapter(adapter);
        expandableListView.setDivider(getResources().getDrawable(R.color.bg_gray_main));
        expandableListView.setDividerHeight(1);

    }

}